<?php
declare(strict_types=1);
 

namespace App\Exchange\Mapper;

use App\Exchange\Model\ExchangePair;
use App\Exchange\Model\ExchangePairList;
use App\Fastapp\Model\Currency;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 交易对Mapper类
 */
class ExchangePairMapper extends AbstractMapper
{
    /**
     * @var ExchangePair
     */
    public $model;

    public function assignModel()
    {
        $this->model = ExchangePair::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 法币id
        if (isset($params['legal_id']) && filled($params['legal_id'])) {
            $query->where('legal_id', '=', $params['legal_id']);
        }

        // 1正常
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        return $query;
    }

    public function genPairList(array $params): bool
    {
        $md = $this->model::query()->find($params['id']);
        $c = Currency::query()->where('id', '!=', $md->legal_id)->where(['type' => 2, 'status' => 1])->pluck('id')->toArray();
        foreach ($c as $cid) {
            try {
                ExchangePairList::query()->create(['legal_id' => $md->legal_id, 'currency_id' => $cid]);
            } catch (\Throwable) {
            }
        }
        return true;
    }
}